package com.sbc.ticket.service.impl;


import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.sbc.cache.DistributedCache;
import com.sbc.common.toolkit.BeanUtil;
import com.sbc.ticket.dao.mapper.TRegionMapper;
import com.sbc.ticket.dao.mapper.TStationMapper;
import com.sbc.ticket.pojo.vo.RegionRespVo;
import com.sbc.ticket.pojo.vo.StationRespVo;
import com.sbc.ticket.service.IRegionStationService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.TimeUnit;

import static com.sbc.ticket.constant.TicketConstant.*;

/**
 * <p>
 * 地区表 服务实现类
 * </p>
 *
 * @author songbaicheng
 * @since 2024-05-16
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class TRegionServiceImpl implements IRegionStationService {

    private final DistributedCache distributedCache;
    private final TStationMapper stationMapper;
    private final TRegionMapper regionMapper;

    @Override
    public List<StationRespVo> listAllStation() {
        return distributedCache.safeGet(
                STATION_ALL,
                List.class,
                () -> BeanUtil.convert(stationMapper.selectList(Wrappers.emptyWrapper()), StationRespVo.class),
                ADVANCE_TICKET_DAY,
                TimeUnit.DAYS
        );
    }

    @Override
    public List<RegionRespVo> listAllRegion() {
        return distributedCache.safeGet(
                REGION_ALL,
                List.class,
                () -> BeanUtil.convert(regionMapper.selectList(Wrappers.emptyWrapper()), RegionRespVo.class),
                ADVANCE_TICKET_DAY,
                TimeUnit.DAYS
        );
    }
}
